package com.dw.dwdzcommerce.base.net;

import java.io.IOException;
import java.util.List;

import com.dw.dwdzcommerce.tools.commons.L;
import com.dw.dwdzcommerce.tools.commons.http.HttpUtilBuilder;
import com.dw.dwdzcommerce.tools.commons.http.IHttpUtil;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * Created by zz on 15/12/2.
 */
public class BaseNetImpl extends BaseNet {

    private static final String TAG = "BaseModuleImpl";
    BaseNetInterface baseModuleInterface;

    @Override
    public void bind(BaseNetInterface baseModuleInterface) {
        this.baseModuleInterface = baseModuleInterface;
    }

    private Handler resultHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case -200:
                    if (null != baseModuleInterface) {
                        baseModuleInterface.onFail((IOException) msg.obj);
                    } else {
                        L.w(TAG, "result handler -200 fail baseModuleInterface is null");
                    }
                    break;
                case 200:
                    if (null != baseModuleInterface) {
                        baseModuleInterface.onDataCallback((String) msg.obj);
                    } else {
                        L.w(TAG, "result handler 200 success baseModuleInterface is null");
                    }
                    break;
            }

        }
    };

    @Override
    public void sendData(String url, List<NameValuePair> values) {
        IHttpUtil iHttpUtil = HttpUtilBuilder.buildOkHttpUtil();
        iHttpUtil.post(url, values, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                L.e(TAG, "onFailure " + Log.getStackTraceString(e.getCause()));
                resultHandler.obtainMessage(-200, e).sendToTarget();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                L.e(TAG, "onResponse " + response.message());
                boolean successful = response.isSuccessful();
                if (successful) {
                    String respResult = null;
                    try {
                        respResult = response.body().string();
                        L.i(TAG, "respResult=" + respResult);
                        resultHandler.obtainMessage(200, respResult).sendToTarget();
                    } catch (IOException e) {
                        e.printStackTrace();
                        resultHandler.obtainMessage(-200, e).sendToTarget();
                    }
                } else {
                    resultHandler.obtainMessage(-200, new IOException("网络访问异常,msg= " + response.message() + "  code= " + response.code())).sendToTarget();
                    L.w(TAG, "onResponse no successful with msg code error");
                }
            }
        });
    }


}
